import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

/**
 *
 * git地址
 *
 * https://gitee.com/yujianlong1988/java8andDisruptor.git
 * @author yujianlong
 * @create 2018-06-16 11:29
 **/
public class LamdaTest0 {




	@Test
	public void predictTest0(){//lambda
		Predicate<String> p1=(String s)->s.contains("1");
//		Predicate<String> p1=s->s.contains("1");
		String words="sdsds1asa";
		String words1="sdsdsasa";
		System.out.println(p1.test(words));
		System.out.println(p1.test(words1));

	}
	//方法引用静态方法
	public static boolean predict(String s){
		return s.contains("1");
	}
	@Test
	public void predictTest1(){//方法引用
		Predicate<String> p1=LamdaTest0::predict;
//		Predicate<String> p1=s->s.contains("1");
		String words="sdsds1asa";
		String words1="sdsdsasa";
		System.out.println(p1.test(words));
		System.out.println(p1.test(words1));

	}

	//过滤符合条件的列表
	public static <E> List<E> filterList(List<E> sourceList,Predicate<E> p){
		List<E> targetList = new ArrayList<>();
		for (E e:sourceList){
			if (p.test(e)) {
				targetList.add(e);
			}
		}
		return targetList;
	}


	@Test
	public void predictTest2(){//方法引用

		List<String> result = filterList(
				Arrays.asList("sss1ss","aaaa","12345"),
				s -> s.contains("1")
		);
		System.out.println(result);

	}

	@Test
	public void functionTest1(){//function测试

		Function<String, Integer> func = s -> s.length();
		System.out.println(func.apply("asdf"));
	}

	@Test
	public void functionTest2(){//function组合

		Function<String, Integer> func1 = s -> s.length();
		Function<Integer, Integer> func2 = i->i*2;
		Function<String, Integer> func3 = func1.andThen(func2);
		System.out.println(func3.apply("asdf"));
	}

}
